import groovy.xml.StreamingMarkupBuilder

import java.nio.file.FileVisitResult
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.SimpleFileVisitor
import java.nio.file.attribute.BasicFileAttributes

String curDir = this.args[0]
String workDir = this.args[1]

println "start generate pom.xml from $curDir to $workDir ..." 

def root = new XmlParser().parse("${curDir}/pom.xml")
Map versions = new HashMap()
root.properties[0].children().each{
	versions.put(it.name().getLocalPart(), it.text())
}

String v = root.version.text()

//		println versions
	
List<File> poms = []
Path path = Paths.get(curDir)
Files.walkFileTree(path, new SimpleFileVisitor<Path>(){
	@Override
	public FileVisitResult visitFile(Path p, BasicFileAttributes attrs) throws IOException {
		if("pom.xml".equals(p.toFile().getName())){
			poms << p.toString()
		}
		return FileVisitResult.CONTINUE;
	}
})

//		println poms

List depInfoList = []
List jars = []
poms.each {
	root = new XmlParser().parse(it)
	if(root.dependencies && root.dependencies[0].dependency){
		root.dependencies[0].dependency.each {
			String groupId = it.groupId[0].text()
			String artifactId = it.artifactId[0].text()
			
			if(!jars.contains(groupId + "." + artifactId)){
				jars << groupId + "." + artifactId
				
				def depInfo = new Object()
				depInfo.metaClass.groupId = groupId
				depInfo.metaClass.artifactId = artifactId
				depInfo.metaClass.version = it.version[0].text()
				
				depInfoList << depInfo
			}
		}
	}
}

StreamingMarkupBuilder builder = new StreamingMarkupBuilder()

String p = "project.build.sourceEncoding"

def project = {
	mkp.declareNamespace('':"http://maven.apache.org/POM/4.0.0")
	mkp.declareNamespace('xsi':"http://www.w3.org/2001/XMLSchema-instance")
	mkp.declareNamespace("schemaLocation':'http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd")
	
	project{
		modelVersion("4.0.0")
		groupId("com.joinway")
		artifactId("framework")
		version("$v")
		
		properties{
			"$p"("UTF-8")
			versions.each {key, value ->
				"$key"("$value")
			}
		}
		
		build{
			plugins{
				plugin{
					groupId("org.codehaus.gmavenplus")
					artifactId("gmavenplus-plugin")
					version("1.4")
					executions{
						execution{
							goals{
								goal("addSources")
								goal("addTestSources")
								goal("generateStubs")
								goal("compile")
								goal("testCompile")
							}
						}
					}
					configuration{
						sources{
							source{
								directory("\${project.basedir}/src/main/java")
								includes{
									include("**/*.groovy")
								}
							}
						}
					}
				}
				plugin{
					groupId("org.apache.maven.plugins")
					artifactId("maven-compiler-plugin")
					version("2.3.2")
					configuration{
						source("1.8")
						target("1.8")
					}
				}
			}
		}
		
		dependencies{
			for(int i = 0; i < depInfoList.size(); i++){
				def node = depInfoList[i]
				dependency{
					groupId("$node.groupId")
					artifactId("$node.artifactId")
					version("$node.version")
				}
			}
			
			dependency{
				groupId("org.fusesource.jansi")
				artifactId("jansi")
				version("1.11")
			}
		}
	}
}

//		println project

FileWriter writer = new FileWriter("${workDir}/pom.xml")
writer << builder.bind(project)

println "generated ${workDir}/pom.xml"